//
//  RTJTabBarController.m
//  RuiTaiJinProject
//
//  Created by jayson on 2018/11/26.
//  Copyright © 2018年 leewei. All rights reserved.
//

#import "RTJTabBarController.h"
#import "RTJUserCenterController.h"
#import "RTJHomeController.h"
#import "RTJLendController.h"
#import "RTJBorrowingController.h"
#import "RTJDiscoverController.h"

@interface RTJTabBarController ()<UITabBarDelegate,UITabBarControllerDelegate>

@property(strong, nonatomic) UINavigationController *webViewNavigationController;
//@property (nonatomic, strong) UITabBarItem *lastItem;

@end

@implementation RTJTabBarController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.tabBar.translucent = NO;
    self.delegate=self;
//    self.lastItem = self.tabBar.selectedItem;
    
    [self setTabViewControllers];
}

#pragma mark - custom methods

-(void)setTabViewControllers
{
    /// 主页面
    UINavigationController *homeVC;
    {
        RTJHomeController *homeRootVC = [[RTJHomeController alloc] init];
        
        homeVC = [[LXNavigationViewController alloc] initWithRootViewController:homeRootVC];
        
        UITabBarItem *item = [self _makeItemWithTitle:@"首页" normalName:@"shouye" selectedName:@"shouyexuanzhong" tag:100];
        
        [homeVC setTabBarItem:item];
    }
    
    /// 借出
    UINavigationController *lendVC;
    {
        RTJLendController *lendRootVC = [[RTJLendController alloc] init];
        
        lendVC = [[LXNavigationViewController alloc] initWithRootViewController:lendRootVC];
        
        UITabBarItem *item = [self _makeItemWithTitle:@"出借" normalName:@"chujie-1" selectedName:@"chujie" tag:101];
        
        [lendVC setTabBarItem:item];
    }
    
   
//    /// 发现
//    UINavigationController *borrowVC;
//    {
//        RTJDiscoverController *borrowRootVC = [[RTJDiscoverController alloc] init];
//
//        borrowVC = [[LXNavigationViewController alloc] initWithRootViewController:borrowRootVC];
//
//        UITabBarItem *item = [self _makeItemWithTitle:@"发现" normalName:@"faxian" selectedName:@"faxianxuanzhong" tag:102];
//
//        [borrowVC setTabBarItem:item];
//    }
 
    
    //    个人页面
    UINavigationController *profileRootVC;
    {
        RTJUserCenterController *profileVC = [[RTJUserCenterController alloc] init];
        
        
        profileRootVC = [[LXNavigationViewController alloc] initWithRootViewController:profileVC];
        
        UITabBarItem *item = [self _makeItemWithTitle:@"我的" normalName:@"wode" selectedName:@"wodexuanzhong" tag:103];
        [profileRootVC setTabBarItem:item];
    }
    
//    self.viewControllers = @[homeVC, lendVC, borrowVC, profileRootVC];
    self.viewControllers = @[homeVC, lendVC, profileRootVC];
}

- (UITabBarItem *)_makeItemWithTitle:(NSString *)aTitle normalName:(NSString *)aNormal selectedName:(NSString *)aSelected tag:(NSInteger)aTag
{
    UITabBarItem *result = nil;
    
    UIImage *nor = [UIImage imageNamed:aNormal];
    UIImage *sel = [UIImage imageNamed:aSelected];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor grayColor], UITextAttributeTextColor,
                                                       nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                       [UIColor colorForHex:@"FFBC04"], UITextAttributeTextColor,
                                                       nil] forState:UIControlStateSelected];
  
    
    if ([UIDevice currentDevice].systemVersion.floatValue >= 7.f) {
        result = [[UITabBarItem alloc] initWithTitle:aTitle image:nor selectedImage:sel];
        [result setTag:aTag];
    } else {
        result = [[UITabBarItem alloc] initWithTitle:aTitle image:nor tag:aTag];
    }
    
    return result;
}

#pragma mark - instance

+(instancetype)sharedInstance
{
    static RTJTabBarController *instance;
    
    if ( ! instance) {
        instance = [[RTJTabBarController alloc] init];
    }
    
    return instance;
}

////双击tabbar实现刷新
//static NSDate *aDate;
//- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
//{
//    double aa = -[aDate timeIntervalSinceDate:[NSDate date]];
//    aDate = [NSDate date];
//
//    // 判断本次点击的UITabBarItem是否和上次的一样
//    if ((item == self.lastItem)&&(item.tag==100||item.tag==101)) { // 一样就发出通知
//        if (aa < 0.5&&aa!=0) {
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"refrshTables" object:nil userInfo:nil];
//        }
//    }
//    // 将这次点击的UITabBarItem赋值给属性
//    self.lastItem = item;
//}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
